package com.sdp.wx.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.wx.db.wx_repair_vehicle_other_expense.entity.WxRepairVehicleOtherExpense;
import com.sdp.wx.db.wx_repair_vehicle_other_expense.service.IWxRepairVehicleOtherExpenseService;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/WxRepairVehicleOtherExpenseController")
public class WxRepairVehicleOtherExpenseController {

	@Autowired
	private IWxRepairVehicleOtherExpenseService iWxRepairVehicleOtherExpenseService;
	
	@Autowired
	private Query query;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iWxRepairVehicleOtherExpenseService, WxRepairVehicleOtherExpense.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody WxRepairVehicleOtherExpense expense) {
		if(expense==null) {
			return Result.error("费用信息为空");
		}
		if(StringUtils.isEmpty(expense.getKeyId())) {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairVehicleOtherExpenseService, WxRepairVehicleOtherExpense.class, new String[] {"expenseName","repairCode"}, "keyId", expense, null, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("费用不能重复");
			}
			iWxRepairVehicleOtherExpenseService.save(expense);
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iWxRepairVehicleOtherExpenseService, WxRepairVehicleOtherExpense.class, new String[] {"expenseName","repairCode"}, "keyId", null, expense, true);
			if(r==null || !r.isSuccess()) {
				return Result.error("费用不能重复");
			}
			iWxRepairVehicleOtherExpenseService.updateById(expense);
		}
		
		return Result.ok();
	}
}

